const path = require("path");
const webpack = require("webpack");
const CopyWebpackPlugin = require('copy-webpack-plugin');
module.exports = {
  runtimeCompiler: true,
  //history:'/' hash:'./'
  publicPath: "/",
  //放置生成的静态资源 (js、css、img、fonts) 的 (相对于 outputDir 的) 目录
  assetsDir: "static",
  //如果你不需要生产环境的 source map，可以将其设置为 false 以加速生产环境构建。
  productionSourceMap: false,
  configureWebpack: {
    resolve: {
      alias: {
        "@assets": path.join(__dirname, "src/assets"),
        "@components": path.join(__dirname, "src/components"),
        "@views": path.join(__dirname, "src/views"),
        "@utils": path.join(__dirname, "src/utils"),
      },
    },
    module: {
      rules: [
        {
          // 匹配所有.html文件
          test: /\.html$/,
          use: [
            {
              // 使用raw-loader处理HTML文件
              // raw-loader可以将文件内容作为字符串导入
              loader: 'raw-loader',
              options: {
                // 禁用ES模块语法，使用CommonJS语法
                // 这样可以避免 "Module parse failed" 的错误
                esModule: false
              }
            }
          ]
        },
        {
          test: /\.geoJson$/,
          type: 'javascript/auto',
          use: [
            {
              loader: 'file-loader',
              options: {
                name: '[name].[ext]',
                outputPath: 'views/arcgis/data/'
              }
            }
          ]
        },
        {
          test: /\.md$/,
          use: [
            {
              loader: 'ignore-loader'
            }
          ]
        }
      ]
    },
    //支持jquery
    plugins: [
      new CopyWebpackPlugin([
        { 
          from: 'public/js/@liveqing/liveplayer/dist/component/crossdomain.xml',
          to: '' 
        },
        { 
          from: 'public/js/@liveqing/liveplayer/dist/component/liveplayer-lib.min.js',
          to: 'js/'
        },
        { 
          from: 'public/js/@liveqing/liveplayer/dist/component/liveplayer.swf',
          to: ''
        }
      ]),
      new webpack.ProvidePlugin({
        $: "jquery",
        jQuery: "jquery",
        "windows.jQuery": "jquery"
      }),
      // 复制HTML文件到构建目录
      new CopyWebpackPlugin([
        { 
          // 源文件：src/views/arcgis/目录下的所有HTML文件
          from: 'src/views/arcgis/*.html',
          // 目标位置：构建目录下的views/arcgis/目录
          // [name]会被替换为原文件名
          // [ext]会被替换为原文件扩展名
          to: 'views/arcgis/[name].[ext]',
          // flatten: true表示忽略源文件的目录结构
          // 直接将文件放在目标目录下
          flatten: true 
        },
        { 
          from: 'src/views/echarts/*.html',
          to: 'views/echarts/[name].[ext]',
          flatten: true 
        },
        { 
          from: 'src/views/html/*.html',
          to: 'views/html/[name].[ext]',
          flatten: true 
        }, { 
          from: 'src/views/arcgis/*.geoJson',
          to: 'views/arcgis/data/[name].[ext]',
          flatten: true 
        },
        { 
          from: 'src/views/components/*.html',
          to: 'views/components/[name].[ext]',
          flatten: true 
        },
        { 
          from: 'src/views/function/*.html',
          to: 'views/function/[name].[ext]',
          flatten: true 
        },
        { 
          from: 'src/views/files/*.html',
          to: 'views/files/[name].[ext]',
          flatten: true 
        },
        { 
          from: 'src/views/openlayer/*.html',
          to: 'views/openlayer/[name].[ext]',
          flatten: true 
        },
        { 
          from: 'src/views/leaflet/*.html',
          to: 'views/leaflet/[name].[ext]',
          flatten: true 
        },
        { 
          from: 'src/views/threejs/*.html',
          to: 'views/threejs/[name].[ext]',
          flatten: true 
        },
        { 
          from: 'src/views/db/*.html',
          to: 'views/db/[name].[ext]',
          flatten: true 
        }
      ]),
    
    ],
  },
  devServer: {
    //被认为是索引文件的文件名
    index: "index.html",
    port: 8019,
    //dev-server在服务器启动后打开浏览器
    open: true,
    // 跨域代理
    proxy: {
      //配置跨域
      "/baseApi": {
        target: "http://localhost:8830",
        changeOrigin: true,
        ws: true,
        pathRewrite: {
          "^/baseApi": "",
        },
      },
      "/arcgisApi": {
        target: "http://localhost:6600",
        changeOrigin: true,
        ws: true,
        pathRewrite: {
          "^/arcgisApi": "",
        },
      },
    },
  },
  pages: {
    index: {
      entry: 'src/main.js',
      template: 'public/index.html',
      filename: 'index.html',
      title: 'Vue示例项目',
      chunks: ['chunk-vendors', 'chunk-common', 'index']
    },
  },
};